﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebRole1
{
    public partial class managePatient : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            bool isAdmin = (bool)Session["isAdmin"];
            if (isAdmin == false)
            {
                Response.Redirect("error.aspx", false);
            }
            Page_Init(sender, e);
        }

        protected void Page_Init(object sender, EventArgs e)
        {
            try
            {
                bool isAdmin = (bool)Session["isAdmin"];
                if (isAdmin == false)
                {
                    Response.Redirect("error.aspx", false);
                }
            }
            catch (Exception) { }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string patientUID = tbx_patientUID.Text;
            Service1 svc = new Service1();
            string result = svc.uploadFaceImage(FileUpload1.FileName, FileUpload1.FileBytes);
            lbl_path.Text = result;
            svc.createPatient_faceImg(patientUID, result);
            Response.Redirect(Request.RawUrl);
        }

        protected void btn_view_Click(object sender, EventArgs e)
        {
            Service1 svc = new Service1();
            System.Drawing.Image objimg = null;
            try
            {
                objimg = System.Drawing.Image.FromStream(svc.retrieveItem(tbx_imageURL.Text), true);

            }
            catch (Exception)
            {
            }

            // must be used to display the image
            Response.Clear();
            Response.ContentType = "image/gif";
            objimg.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
        }
   
        protected void SqlDataSource3_Inserted(object sender, SqlDataSourceStatusEventArgs e)
        {
            //int retvalue = Convert.ToInt32(e.Command.Parameters["@uid"].Value);
            if (e.Exception == null)
            {
                lblMessage.Text = String.Format(
                        "Patient '{0}' successfully added.",
                        e.Command.Parameters["@uid"].Value);
                tbx_patientUID.Text = e.Command.Parameters["@uid"].Value.ToString();
            }
            else
            {
                lblMessage.Text = "Unable to add.";
                e.ExceptionHandled = true;
            }
        }

       
    }
}